package com.liuheng.spzx.cart.service.serviceImpl;

import com.liuheng.spzx.cart.service.CartService;
import com.liuheng.spzx.feign.product.ProductFeign;
import com.liuheng.spzx.model.entity.h5.CartInfo;
import com.liuheng.spzx.model.entity.product.Product;
import com.liuheng.spzx.model.entity.product.ProductSku;
import com.liuheng.spzx.model.entity.user.UserInfo;
import com.liuheng.util.AuthContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ProductFeign productFeign;

    private String getCartKey(Long userId) {
        //定义key user:cart:userId
        return "user:cart:" + userId;
    }
    @Override
    public void addToCart(Long skuId, Integer skuNum) {
        UserInfo userInfo = AuthContextUtil.getUserInfo();
        Long userId = userInfo.getId();
        CartInfo cartInfo = (CartInfo) redisTemplate.opsForHash().get(getCartKey(userId), String.valueOf(skuId));
        if(cartInfo!=null){
            cartInfo.setSkuId(skuId);
            cartInfo.setSkuNum(cartInfo.getSkuNum()+skuNum);
            cartInfo.setIsChecked(1);
        }else {
            ProductSku productSku=productFeign.getBySkuId(skuId);
             cartInfo = new CartInfo();
            cartInfo.setCartPrice(productSku.getSalePrice());
            cartInfo.setSkuNum(skuNum);
            cartInfo.setSkuId(skuId);
            cartInfo.setUserId(userId);
            cartInfo.setImgUrl(productSku.getThumbImg());
            cartInfo.setSkuName(productSku.getSkuName());
            cartInfo.setIsChecked(1);
            cartInfo.setCreateTime(new Date());
            cartInfo.setUpdateTime(new Date());
        }
        redisTemplate.opsForHash().put(getCartKey(userId),String.valueOf(skuId),cartInfo);
    }

    @Override
    public List<CartInfo> getCartList() {
        UserInfo userInfo = AuthContextUtil.getUserInfo();
        List values = redisTemplate.opsForHash().values(getCartKey(userInfo.getId()));
        return values;
    }

    @Override
    public void deleteCartBySkuId(Long skuId) {
        UserInfo userInfo = AuthContextUtil.getUserInfo();
        redisTemplate.opsForHash().delete(getCartKey(userInfo.getId()),String.valueOf(skuId));
    }

    @Override
    public void checkCart(Long skuId, Integer isChecked) {
        UserInfo userInfo = AuthContextUtil.getUserInfo();
        Boolean hasKey = redisTemplate.opsForHash().hasKey(getCartKey(userInfo.getId()), String.valueOf(skuId));
        if (hasKey){
            CartInfo cartInfo = (CartInfo) redisTemplate.opsForHash().get(getCartKey(userInfo.getId()), String.valueOf(skuId));
            cartInfo.setIsChecked(isChecked);
            redisTemplate.opsForHash().put(getCartKey(userInfo.getId()),String.valueOf(skuId),cartInfo);
        }
    }

    @Override
    public void checkAllCart(Integer isChecked) {
        UserInfo userInfo = AuthContextUtil.getUserInfo();
        List values = redisTemplate.opsForHash().values(getCartKey(userInfo.getId()));
        for (Object value : values) {
            CartInfo cartInfo = (CartInfo) value;
            if (cartInfo.getIsChecked()!=isChecked) {
                cartInfo.setIsChecked(isChecked);
            }
            redisTemplate.opsForHash().put(getCartKey(userInfo.getId()),String.valueOf(cartInfo.getSkuId()),cartInfo);
        }
    }

    @Override
    public void clearCart() {
        Long userId = AuthContextUtil.getUserInfo().getId();
        String cartKey = getCartKey(userId);
        redisTemplate.delete(cartKey);
    }

    @Override
    public List<CartInfo> getAllChecked() {
        Long userId = AuthContextUtil.getUserInfo().getId();
        String cartKey = getCartKey(userId);
        List<CartInfo> cartInfoList = new ArrayList<>();
        List list = redisTemplate.opsForHash().values(cartKey);
        for (Object o : list) {
            CartInfo cartInfo = (CartInfo) o;
            if (cartInfo.getIsChecked()==1){
                cartInfoList.add(cartInfo);
//                redisTemplate.opsForHash().delete(cartKey,String.valueOf(cartInfo.getSkuId()));
            }
        }
        return cartInfoList;
    }

    @Override
    public void deleteChecked() {
        Long userId = AuthContextUtil.getUserInfo().getId();
        String cartKey = getCartKey(userId);
        List list = redisTemplate.opsForHash().values(cartKey);
        for (Object o : list) {
            CartInfo cartInfo = (CartInfo) o;
            if (cartInfo.getIsChecked()==1){
                redisTemplate.opsForHash().delete(cartKey,String.valueOf(cartInfo.getSkuId()));
            }
        }
    }
}
